#include "event_driver.h"

#include <string>
#include <iostream>
#include <boost/date_time.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include "tcp_client.h"
#include "tcp_server.h"


using namespace std;
using namespace auroral::event;

class Client:public auroral::net::TcpClient
{
public:
	Client(boost::asio::io_service& io_service,
		const std::string& server, int16_t port)
		:TcpClient(io_service, server,  port) {};
protected:
	void process_read()
	{
		cout << &response_;
	} 
};

int onEnter(EventDriver &eventDriver, string eventType, shared_ptr<void> data)
{
	cout << "onEnter" << endl;
	return 0;
}

int onQuit(EventDriver &eventDriver, string, shared_ptr<void> data)
{
	cout << "onQuit" << endl;
	return 0;
}

int onIdle(EventDriver &eventDriver, string, shared_ptr<void>)
{
	cout << "idle" << endl;
	return 0;
}

int onPing(EventDriver &eventDriver, string, shared_ptr<void> data)
{
	shared_ptr<string> str = boost::static_pointer_cast<string>(data);
	shared_ptr<string> ret = shared_ptr<string>(new string);
	*ret = *str;
	cout << "ping \"" << *str << "\"" << endl;
	eventDriver.emitEvent("pong", ret);
	return 0;
}

int onPong(EventDriver &eventDriver, string, shared_ptr<void> data)
{
	shared_ptr<string> str = boost::static_pointer_cast<string>(data);
	cout << "pong \"" << *str << "\""<< endl;
	return 0;
}

int onQuit2(EventDriver &eventDriver, string, shared_ptr<void> data)
{
	cout << "onQuit2" << endl;
	return 0;
}

int doQuit(EventDriver &eventDriver, string, shared_ptr<void> data)
{
	cout << "doQuit" <<endl;
	eventDriver.quit();
	return 0;
}


int main(){
	/*
	using namespace boost::gregorian;
	using namespace boost::posix_time;
	cout << to_simple_string(microsec_clock::local_time()) << endl;
	boost::thread::sleep(microsec_clock::universal_time() + seconds(1));
	*/
	
	EventDriver driver;
	driver.registerEvent("ping");
	driver.registerEvent("pong");
	driver.registerEvent("idle");
	driver.registerEvent("doQuit");

	driver.addHandle("enter", onEnter, 0);
	driver.addHandle("quit", onQuit, 0);
	driver.addHandle("quit", onQuit2, 10);
	driver.addHandle("ping", onPing, 0);
	driver.addHandle("pong", onPong, 0);
	driver.addHandle("idle", onIdle, 0);
	driver.addHandle("doQuit", doQuit, 0);
	
	for (int i=0; i < 30; i++)
	{
		driver.emitEvent("idle", shared_ptr<void>());
		shared_ptr<string> str = shared_ptr<string>(new string("Test Ping 5489432415"));
		driver.emitEvent("ping", str);
	}
	driver.emitEvent("doQuit");
	driver.exec();
	boost::asio::io_service io_service1;
	Client client(io_service1, "www.google.com", 80);
	client.active();
	boost::thread thread1(boost::bind(&boost::asio::io_service::run, &io_service1));
	//boost::this_thread::sleep(boost::posix_time::seconds(3));
	client.async_write(
		"GET / HTTP/1.0\r\n"
		"Host: www.google.com\r\n"
		"Accept: */*\r\n"
		"Connection: close\r\n\r\n");
	thread1.join();
	return 0;
}
